package com.bw.shopping.mvvm.viewmodel;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.bw.greendao.dao.DaoSessionUtils;
import com.bw.greendao.dao.ShoppEntityDao;
import com.bw.greendao.dao.UserEntityDao;
import com.bw.greendao.entity.ShoppDaoEntity;
import com.bw.greendao.entity.ShoppEntity;
import com.bw.greendao.entity.UserDaoEntity;
import com.bw.greendao.entity.UserEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.shopping.adapter.CommodityAdapter;
import com.bw.shopping.bind.CommdityBindRv;
import com.bw.shopping.entity.CommodityEntity;
import com.bw.shopping.entity.MerchEntity;
import com.bw.shopping.mvvm.model.CommodityModel;
import com.bw.shopping.mvvm.view.CommodityActivity;
import com.bw.shopping.mvvm.view.ShoppFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Observable;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/21 10:52
 */
public class CommodityViewModel extends BaseViewModel<CommodityModel> {

    public ObservableField<String> img = new ObservableField<>();
    public ObservableField<String> text = new ObservableField<>();
    public BaseQuickAdapter adapter = new CommodityAdapter();
     CommodityEntity addshop = new CommodityEntity();
     CommodityEntity off_line_shop = new CommodityEntity();

    int shopId;

    @Override
    public void oncreate() {
        super.oncreate();
        //请求商品数据
        HashMap<String,Object> map = new HashMap<>();
        map.put("categoryId","14");
        map.put("pageNo",1);
        m.requestCommodity(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);

        HashMap<String, Object> maps = new HashMap<>();
        maps.put("goodsId",shopId);
        m.requestMerch(maps)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);

    }


    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity instanceof CommodityEntity){
            CommodityEntity commodityEntity = (CommodityEntity) baseEntity;
           off_line_shop =  commodityEntity;
            addshop = (CommodityEntity) baseEntity;

            adapter.setNewInstance(commodityEntity.getData());


        }

        if(baseEntity instanceof MerchEntity){
            MerchEntity baseEntity1 = (MerchEntity) baseEntity;
            img.set(baseEntity1.getData().getGoods_detail_two());
            text.set(baseEntity1.getData().getGoods_desc());
        }


    }

    public void bundle(int id){
        shopId = id;
    }

    public void backgoods(){
        finishPage();
    }

    //加入购物车
    public void addShop(){

        List<UserEntity> list = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();
        if(list.get(0).getFlag()){
            HashMap<String, Object> map = new HashMap<>();
            map.put("goodsDesc",addshop.getData().get(0).getGoods_desc());
            map.put("goodsIcon",addshop.getData().get(0).getGoods_default_icon());
            map.put("goodsId",addshop.getData().get(0).getCategory_id());
            map.put("goodsPrice",addshop.getData().get(0).getGoods_default_price());
            map.put("goodsSku",addshop.getData().get(0).getGoods_default_sku());
            map.put("time",System.currentTimeMillis());
            map.put("userId",list.get(0).getUser_id());
            m.requestAddShopp(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
            showMsg("添加购物车成功");
        }else {

            ShoppEntity shoppEntity = new ShoppEntity();
            shoppEntity.setGoods_desc(off_line_shop.getData().get(0).getGoods_desc());
            shoppEntity.setGoods_default_icon(off_line_shop.getData().get(0).getGoods_default_icon());
            shoppEntity.setGoods_id(off_line_shop.getData().get(0).getId());
            shoppEntity.setGoods_price(off_line_shop.getData().get(0).getGoods_default_price());
            shoppEntity.setGoods_default_sku(off_line_shop.getData().get(0).getGoods_default_sku());
            new ShoppDaoEntity().insert(shoppEntity);
            LogUtils.e("ShoppDaoEntity"+shoppEntity.getGoods_default_icon());
            showMsg("添加离线购物车成功");
        }

    }
}
